#include "readfile.h"

#include <QFileDialog>
#include <QtConcurrent>
#include <QDebug>
#include "qmltableview.h"

ReadFile::ReadFile(QObject *parent) : QObject(parent)
{
    initFileWatch();
}

void ReadFile::sltFileChange(const QStringList filePathList, FileWatch::FileState flag)
{

}

void ReadFile::sltSearchFile(const QString searchData)
{
     emit sigShowResult(nullptr);

    qDebug() << "searchData:" << searchData;

    QMap<QString, QString>::const_iterator it = m_FileDataList.begin();

    while(it != m_FileDataList.end())
    {

        if( it.value().contains(searchData))
        {
            pRowData pData = new rowData();

            pData->data = it.value();
            pData->title = it.key();
            emit sigShowResult(pData);
        }

        it++;
    }
}

#include <qt_windows.h>

void ReadFile::initFileWatch()
{
    QString  filePath = QFileDialog::getExistingDirectory(nullptr, "read File Path","",QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);

    qDebug() << filePath;

    m_pFileWatch = new FileWatch(filePath, this);

    QStringList stList = m_pFileWatch->getFileList();


     qDebug() << "start readData";
     LARGE_INTEGER litmp;
     LONGLONG QPart1, QPart2;
     double  dfFreq = 0, dfTime = 0;
     //计算程序的执行时间
     QueryPerformanceFrequency(&litmp);///>取得高精度运行计数器的频率f
     dfFreq = static_cast<double>(litmp.QuadPart);

     QueryPerformanceCounter(&litmp);
     QPart1 = litmp.QuadPart;///>开始计时


     QTime time;
     time.start();

     //qt5.5无法使用成员函数
//     QFuture<QMap<QString, QString>> future = QtConcurrent::run(this, &ReadFile::loadAllFiles,stList);

     QFuture<QMap<QString, QString>> future = QtConcurrent::run(loadAllFiles,stList);
     future.waitForFinished(); //使用这个数据量过大会阻塞系统
     m_FileDataList = future.result();
//     m_FileDataList = loadAllFiles(stList);

//     while(1)
//     {

//         delayms(30);

////         //无法使用系统的方法暂停或者停止
////         future.pause();
////         break;

//         if(future.isFinished())
//         {
//             m_FileDataList = future.result();
//             break;
//         }
//     }
     QueryPerformanceCounter(&litmp);
     QPart2 = litmp.QuadPart;///>终止计时
     dfTime = (static_cast<double>(QPart2 - QPart1) / dfFreq) * 1000;///>单位为秒*1000000 us

     time.elapsed();
    qDebug() << "end readData" << dfTime <<  time.elapsed() << m_FileDataList.size();



}

void ReadFile::delayms(int ms)
{
    QEventLoop loop;
    QTimer::singleShot(ms,&loop,&QEventLoop::quit);
    loop.exec();


}

QMap<QString, QString> loadAllFiles(const QStringList filePathList)
{

    QMap<QString, QString> fileDataMap;
    foreach(QString filePath, filePathList)
    {
        QFile file(filePath);
        if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
        {


            continue;
        }

//        qDebug() << "add files"<< filePath;

      fileDataMap.insert(filePath,QString(file.readAll()) );
      file.close();

//      QThread::msleep(200);

    }

    qDebug() << "end loadFile";
    return  fileDataMap;
}

